package com.ubst.smartad.impl;

import java.sql.*;
import java.util.*;
import java.io.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSASysGroupPolicyDO;
import com.ubst.smartad.common.TimeUtil;
import com.ubst.smartad.logger.Debug;

public class TSASysGroupPolicyMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;

	/** 
     * 생성자
     *   
     *   
    */ 
	public TSASysGroupPolicyMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSASysGroupPolicyMngImpl", log_level);

	}
	
    /** 
	 * @author KJH
     * 계정관리 그룹 정책 등록
     *   
     */  
    public void insertSysGroupPolicy(TSASysGroupPolicyDO sysGroupPolicyDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_SYSGROUP_POLICY " )
				.append(" ( ")
				.append("  GROUPID ,SYSTYPE ,ROOTLOGIN ,RETRYBLOCK ,RETRYSUSPEND ")
				.append(" ,UNUSEDACCOUNT ,SESSIONCOUNT ,PASSMINLFLAG ,PASSMAXLFLAG ,PASSHISFLAG ")
				.append(" ,PASSUSERNAME ,PASSCOMPLEXITY ,CHANGEPASS ,LOGOFFHOUR ,ENABLEADMIN ")
				.append(" ,ENABLEGUEST ,PASSDIFFFLAG ,PASSSEQFLAG ,PASSDICFLAG ,OLDPASSFLAG ")
				.append(" ,CRESULT ,NSTATUS ,LASTSYNC ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ")
				.append(" ) ");

			debug.debug("insertSysGroupPolicy ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, sysGroupPolicyDO.groupid);
			pstmt.setString(idx++, sysGroupPolicyDO.systype);
			pstmt.setString(idx++, sysGroupPolicyDO.rootlogin);
			pstmt.setString(idx++, sysGroupPolicyDO.retryblock);
			pstmt.setString(idx++, sysGroupPolicyDO.retrysuspend);
			pstmt.setString(idx++, sysGroupPolicyDO.unusedaccount);
			pstmt.setString(idx++, sysGroupPolicyDO.sessioncount);
			pstmt.setString(idx++, sysGroupPolicyDO.passminlflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passmaxlflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passhisflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passusername);
			pstmt.setString(idx++, sysGroupPolicyDO.passcomplexity);
			pstmt.setString(idx++, sysGroupPolicyDO.changepass);
			pstmt.setString(idx++, sysGroupPolicyDO.logoffhour);
			pstmt.setString(idx++, sysGroupPolicyDO.enableadmin);
			pstmt.setString(idx++, sysGroupPolicyDO.enableguest);
			pstmt.setString(idx++, sysGroupPolicyDO.passdiffflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passseqflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passdicflag);
			pstmt.setString(idx++, sysGroupPolicyDO.oldpassflag);
			pstmt.setString(idx++, sysGroupPolicyDO.cresult);
			pstmt.setInt(idx++, sysGroupPolicyDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

    /** 
	 * @author KJH
     * 계정관리 그룹 정책 변경
     *   
     */  
    public void updateSysGroupPolicy(TSASysGroupPolicyDO sysGroupPolicyDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_SYSGROUP_POLICY " )
				.append(" SET ")
				.append("  SYSTYPE=? ,ROOTLOGIN=? ,RETRYBLOCK=? ,RETRYSUSPEND=? ,UNUSEDACCOUNT=? ")
				.append(" ,SESSIONCOUNT=? ,PASSMINLFLAG=? ,PASSMAXLFLAG=? ,PASSHISFLAG=? ,PASSUSERNAME=? ")
				.append(" ,PASSCOMPLEXITY=? ,CHANGEPASS=? ,LOGOFFHOUR=? ,ENABLEADMIN=? ,ENABLEGUEST=? ")
				.append(" ,PASSDIFFFLAG=? ,PASSSEQFLAG=? ,PASSDICFLAG=? ,OLDPASSFLAG=? ,CRESULT=? ")
				.append(" ,NSTATUS=? ,LASTSYNC=? ")
				.append(" WHERE GROUPID=? ");

			debug.debug("updateSysGroupPolicy ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, sysGroupPolicyDO.systype);
			pstmt.setString(idx++, sysGroupPolicyDO.rootlogin);
			pstmt.setString(idx++, sysGroupPolicyDO.retryblock);
			pstmt.setString(idx++, sysGroupPolicyDO.retrysuspend);
			pstmt.setString(idx++, sysGroupPolicyDO.unusedaccount);
			pstmt.setString(idx++, sysGroupPolicyDO.sessioncount);
			pstmt.setString(idx++, sysGroupPolicyDO.passminlflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passmaxlflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passhisflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passusername);
			pstmt.setString(idx++, sysGroupPolicyDO.passcomplexity);
			pstmt.setString(idx++, sysGroupPolicyDO.changepass);
			pstmt.setString(idx++, sysGroupPolicyDO.logoffhour);
			pstmt.setString(idx++, sysGroupPolicyDO.enableadmin);
			pstmt.setString(idx++, sysGroupPolicyDO.enableguest);
			pstmt.setString(idx++, sysGroupPolicyDO.passdiffflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passseqflag);
			pstmt.setString(idx++, sysGroupPolicyDO.passdicflag);
			pstmt.setString(idx++, sysGroupPolicyDO.oldpassflag);
			pstmt.setString(idx++, sysGroupPolicyDO.cresult);
			pstmt.setInt(idx++, sysGroupPolicyDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));
			pstmt.setString(idx++, sysGroupPolicyDO.groupid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

	/**
	 * @author : KJH 
	 * 계정관리 그룹 정책 출력
	 *
	 */
	public TSASysGroupPolicyDO selectSysGroupPolicy(String groupid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSASysGroupPolicyDO sysGroupPolicyDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  SYSTYPE ,ROOTLOGIN ,RETRYBLOCK ,RETRYSUSPEND ,UNUSEDACCOUNT ")
				.append(" ,SESSIONCOUNT ,PASSMINLFLAG ,PASSMAXLFLAG ,PASSHISFLAG ,PASSUSERNAME ")
				.append(" ,PASSCOMPLEXITY ,CHANGEPASS ,LOGOFFHOUR ,ENABLEADMIN ,ENABLEGUEST ")
				.append(" ,PASSDIFFFLAG ,PASSSEQFLAG ,PASSDICFLAG ,OLDPASSFLAG ,CRESULT ")
				.append(" ,NSTATUS ,LASTSYNC ")
				.append(" FROM TSA_SYSGROUP_POLICY ")
				.append(" WHERE GROUPID=? ");

			debug.debug("selectSysGroupPolicy ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, groupid);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				sysGroupPolicyDO = new TSASysGroupPolicyDO();
				sysGroupPolicyDO.groupid = groupid;
				sysGroupPolicyDO.systype = rs.getString("SYSTYPE");
				sysGroupPolicyDO.rootlogin = rs.getString("ROOTLOGIN");
				sysGroupPolicyDO.retryblock = rs.getString("RETRYBLOCK");
				sysGroupPolicyDO.retrysuspend = rs.getString("RETRYSUSPEND");
				sysGroupPolicyDO.unusedaccount = rs.getString("UNUSEDACCOUNT");
				sysGroupPolicyDO.sessioncount = rs.getString("SESSIONCOUNT");
				sysGroupPolicyDO.passminlflag = rs.getString("PASSMINLFLAG");
				sysGroupPolicyDO.passmaxlflag = rs.getString("PASSMAXLFLAG");
				sysGroupPolicyDO.passhisflag = rs.getString("PASSHISFLAG");
				sysGroupPolicyDO.passusername = rs.getString("PASSUSERNAME");
				sysGroupPolicyDO.passcomplexity = rs.getString("PASSCOMPLEXITY");
				sysGroupPolicyDO.changepass = rs.getString("CHANGEPASS");
				sysGroupPolicyDO.logoffhour = rs.getString("LOGOFFHOUR");
				sysGroupPolicyDO.enableadmin = rs.getString("ENABLEADMIN");
				sysGroupPolicyDO.enableguest = rs.getString("ENABLEGUEST");
				sysGroupPolicyDO.passdiffflag = rs.getString("PASSDIFFFLAG");
				sysGroupPolicyDO.passseqflag = rs.getString("PASSSEQFLAG");
				sysGroupPolicyDO.passdicflag = rs.getString("PASSDICFLAG");
				sysGroupPolicyDO.oldpassflag = rs.getString("OLDPASSFLAG");
				sysGroupPolicyDO.cresult = rs.getString("CRESULT");
				sysGroupPolicyDO.nstatus = rs.getInt("NSTATUS");
				sysGroupPolicyDO.lastsync = rs.getString("LASTSYNC");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return sysGroupPolicyDO;
	}

    /** 
	 * @author KJH
     * 계정관리 그룹 정책 삭제
     *   
     */  
    public void deleteSysGroupPolicy(String groupid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_SYSGROUP_POLICY " )
				.append(" WHERE GROUPID=? ");

			debug.debug("deleteSysGroupPolicy ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, groupid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

}
